package com.woniuxy.receivables.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.request.ResPage;
import com.woniuxy.commons.vo.AddAddTimeToReceivablesVo;
import com.woniuxy.commons.vo.SearchProjectVo;
import com.woniuxy.receivables.mapper.ReceivablesMapper;
import com.woniuxy.commons.entity.Receivables;
import com.woniuxy.receivables.service.ReceivablesService;
import com.woniuxy.receivables.vo.InvestsVo;
import com.woniuxy.receivables.vo.ReceivablesVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ReceivablesServiceImpl implements ReceivablesService {

    @Resource
    private ReceivablesMapper receivablesMapper;

    @Override
    public boolean refund(String pno) {
        receivablesMapper.refundReceivables(pno);
        List<Receivables> receivablesList = receivablesMapper.findAllReceivablesByPno(pno);
        receivablesList.forEach(receivables -> {
            receivablesMapper.refundToUserDetails(receivables);
        });
        return false;
    }

    @Override
    public boolean setAddTime(AddAddTimeToReceivablesVo addAddTimeToReceivablesVo) {
        return receivablesMapper.setAddTime(addAddTimeToReceivablesVo);
    }

    @Override
    public List<Receivables> findAllReceivablesByPno(String pno) {
        return receivablesMapper.findAllReceivablesByPno(pno);
    }

    @Override
    public ResPage<List<InvestsVo>> searchInvests(SearchProjectVo searchProjectVo) {
        //  分页
        PageHelper.startPage(searchProjectVo.getPage(), searchProjectVo.getSize());
        List<InvestsVo> list = receivablesMapper.searchInvests(searchProjectVo);
        // 获取分页信息
        PageInfo<InvestsVo> pageInfo = new PageInfo<>(list);
        ResPage<List<InvestsVo>> resPage = new ResPage<>();
        resPage.setCurPage(pageInfo.getPageNum());
        resPage.setSize(pageInfo.getSize());
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(pageInfo.getList());
        return resPage;
    }

    @Override
    public ResPage<List<ReceivablesVo>> searchReceivables(SearchProjectVo searchProjectVo) {
        //  分页
        PageHelper.startPage(searchProjectVo.getPage(), searchProjectVo.getSize());
        List<ReceivablesVo> list = receivablesMapper.searchReceivables(searchProjectVo);
        // 获取分页信息
        PageInfo<ReceivablesVo> pageInfo = new PageInfo<>(list);
        ResPage<List<ReceivablesVo>> resPage = new ResPage<>();
        resPage.setCurPage(pageInfo.getPageNum());
        resPage.setSize(pageInfo.getSize());
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(pageInfo.getList());
        return resPage;
    }
}
